// $Id$
package de.servicezombie.pub.r1;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import de.servicezombie.lang.ErrorCode;

/**
 * better name would be errorContext, but easier to find and shorter in 
 * json / xml output
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
		"returnCode", "debugText"
})
@XmlRootElement(name = "sys", namespace = XmlNamespaces.SERVICEZOMBIE_COMMON)
public class Sys {

	private static ExceptionForWebResponseTransformer defaultExceptionToReturnCodeTransformer = new ExceptionForWebResponseTransformerDefaultImpl();
	
	public static void setDefaultExceptionToReturnCodeTransformer(
			ExceptionForWebResponseTransformer defaultExceptionToReturnCodeTransformer) {
		Sys.defaultExceptionToReturnCodeTransformer = defaultExceptionToReturnCodeTransformer;
	}
	
	@XmlElement(required = true)
	private ErrorCode returnCode = ErrorCode.OK;
	
	@XmlElement(required = false)
	private String debugText = null;
	
	Sys() {}
	
	public ErrorCode getReturnCode() {
		return returnCode;
	}

	public String getDebugText() {
		return debugText;
	}

	public void setErrorContext(Throwable exception, ExceptionForWebResponseTransformer transformer) {
		this.debugText = transformer.transformToMessage(exception);
		this.returnCode = transformer.transformToErrorCode(exception);
	}
	
	public void setErrorContext(Throwable exception) {
		setErrorContext(exception, defaultExceptionToReturnCodeTransformer);			
	}
	
	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
				.append("rc", returnCode)
				.append("debugText", debugText)
				.toString();
	}

}
